package lib;

import java.awt.Image;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import pojo.Book;
import pojo.Link;
import pojo.Website;
import dao.BookDAO;
import dao.LinkDAO;
import dao.LogDAO;
import dao.WebsiteDAO;

public class WebsiteRunnable implements Runnable {

	private Website website;
	private String webUrl;
	private Link linkGetBook;
	private boolean flagBook=true;
	public WebsiteRunnable(Website w)
	{
		website=w;
		webUrl=w.getWebsite();
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		WebsiteDAO dao = new WebsiteDAO();
		
		List<Website> list= dao.getList();
		List<String> name;
		List<String> link;

		LinkDAO linkDAO= new LinkDAO();
		System.out.println(website.getWebsite());
		if(website.getWebType().equals("1"))
			{
				OneMenuNoClickFilter fil= new OneMenuNoClickFilter();
				fil.lay_noi_dung_web2(website.getWebsite(),website.getMenu1(), website.getMenu2());
				name= fil.getTopicDic();
				link=fil.getLink();
			}
			else
				if(website.getWebType().equals("2"))
				{
					OneMenuHasClickFilter fil= new OneMenuHasClickFilter();
					fil.lay_noi_dung_web2(website.getWebsite(),website.getMenu1(), website.getMenu2());
					name= fil.getTopicDic();
					link=fil.getLink();
				}
				else
				{
					TwoMenuFilter fil= new TwoMenuFilter();
					fil.lay_noi_dung_web(website.getWebsite(),website.getMenu1(), website.getMenu2());
					name= fil.getTopicDic();
					link=fil.getLink();
				}
			
			int len=name.size();
			int dem= 0;
			while(dem<len)
			{
				String stt=name.get(dem);
				if(stt==null||stt.equals(""))
				{
					name.remove(dem);
					link.remove(dem);
					len--;
				}
				else
					dem++;
			}
			
			List<Link> list_link= new ArrayList<Link>();
			for(int j= 0;j<name.size();j++)
			{
				Link t_link= new Link();
				t_link.setKeywords(name.get(j));
				t_link.setLinkUrl(link.get(j));
				t_link.setWebsite(website);
				list_link.add(t_link);
			}
			
			LoadLinksAndMapToTopics loadlink= new LoadLinksAndMapToTopics();
			list_link=loadlink.insertLinks(list_link);
			/// da insert link vao data base
			
			this.getbook(list_link);
			
	}
		
		
	
	
	public void getbook(List<Link> list_link2)
	{  
		LinkDAO dao = new LinkDAO();
		List<Link> list= list_link2;
		List<Element> list1;
		ConnectionWeb con= new ConnectionWeb();
		String urlWeb=list.get(0).getWebsite().getWebsite();
		for(int i=0; i<list.size();i++)
		{
			Link t= list.get(i);
			System.out.println(t.getKeywords());
			this.linkGetBook=t;
			String url = t.getLinkUrl();
			Document root1= con.getInfo(url);
			if(root1==null) continue;
			String turl1=url.replaceAll("/","");
			String turl2=con.getUrl();
			turl2=turl2.replaceAll("/","");
			if(turl1.equals(turl2)==false)
			{
				url=con.getUrl();
			}
			List<String> list_link=new ArrayList<String>();

			if(root1==null)
			{
				
			}
			else
			{	
				String xp_page= t.getWebsite().getPage();
			    String xp_web=t.getWebsite().getWebContent();
			   // xp_book_content=StringEdit.EditXpath(xp_book_content);
				//Object[] list_node = root1.evaluateXPath(xp_page);

			    List<Element> list_node = con.executeXpath(root1, xp_page, 0);
			    
				if(list_node.size()>0)
				{
					if(list_link!=null) list_link.clear();
					 Element root2= list_node.get(0);
					 list1= root2.children();
					 String link_hien_tai=url;
					 String link_ke_tiep=null;
					 int index=0;		
					 String trang_hien_tai="1";				 
					 boolean flag=false; 
					 int tDem=1;
					 while(flag==false)
					 {		
						 list_link.add(link_hien_tai);
						 System.out.print(tDem);
						 tDem++;
						 System.out.print(" ");
						 for(int j=0; j< list1.size();j++)
						 {
							 Element t1= list1.get(j);
							 String sNum= t1.text();
							 sNum= StringEdit.ChuanHoa(sNum);
							 sNum=StringEdit.removeNotNum(sNum);
							 if(sNum.equals(trang_hien_tai)==true)
							 {
								if(j<list1.size()-1)
								{
									Element t2= list1.get(j+1);
									List<Element> list2=t2.getElementsByTag("a");
									if(list2.size()!=0||t2.tagName().equals("a")==true)
									{
										Element t3 ;
										if(list2.size()>0)
											t3=  list2.get(0);
										else t3=t2;
										link_ke_tiep=t3.attr("href");
										if(link_ke_tiep==null||link_ke_tiep.equals(""))
										{
											flag=true;
											String noidung=t.getWebsite().getWebsite() + " "+t.getKeywords()+ "\n" + " khÃ´ng láº¥y Ä‘Æ°á»£c trang káº¿ tiáº¿p";
											LogDAO.insert(noidung);
										}
										trang_hien_tai=t3.text();
										trang_hien_tai= StringEdit.ChuanHoa(trang_hien_tai);
										trang_hien_tai=StringEdit.removeNotNum(trang_hien_tai);
										break;
									}
									else
										flag= true;
									
								}
								if(j==list1.size()-1) flag=true;
							 }
							 
						 }
						 
						 if(flag==true) break;
						 if(link_ke_tiep==null) break;
						 String temp_linkkt=link_ke_tiep;
						 link_ke_tiep=StringEdit.EditLink(urlWeb, link_ke_tiep);
						 if(con.TryConnect(link_ke_tiep)==false)
						 {
							 link_ke_tiep=url+temp_linkkt;
						 }
						 link_hien_tai=link_ke_tiep;
						 
						Document troot= con.getInfo(link_ke_tiep);
						if(troot==null)
						{
							String tlinkurl=link_ke_tiep.replaceAll(" ","");
							tlinkurl=tlinkurl.replaceAll("\t","");
							troot= con.getInfo(tlinkurl);
							link_ke_tiep=tlinkurl;
						}
						if(troot==null) 
						{
							//ghi log khong lay dc thong tin tu xpath
						}
						else
						{
						//Object[] t_list_node = troot.evaluateXPath(xp_page);
							List<Element> t_list_node = con.executeXpath(troot, xp_page, 0);
							if(t_list_node.size()>0)
							{
								Element temp=(Element) t_list_node.get(0);
								list1= temp.children();
							}	
						}
					 }
					 
					 // da co list link cac trang cua 1 loai chu de sach
					 
					 System.out.println();
					 if(list_link.size()==0)
					 {
						 //ghi log link chu de khong co trang nao ca
						 
					 }
					 else
					 {
						 int l_link_size=100;
						 if(list_link.size()<l_link_size)
						 {
							 l_link_size=list_link.size();
									 
						 }
						 for(int j=0; j< l_link_size;j++)
						 {
							 String link_book=list_link.get(j);
							 int tindex=j+1;
							System.out.println("trang "+tindex);
							
							 String xp_webcontent=t.getWebsite().getWebContent();
							 Document book_root1=con.getInfo(link_book);
							 
							 List<Element> l_book_xp=con.executeXpath(book_root1, xp_webcontent, 1);
							 
							 for(int chiso=0;chiso<l_book_xp.size();chiso++)
							 {
								 Element webContent=l_book_xp.get(chiso);
								 								 
								 List<Element> list_book=webContent.getElementsByTag("a");
								 if(list_book!=null && list_book.size()>0)
								 {
									 String urlbook1=list_book.get(0).attr("href");
									 
										String turlbook1=StringEdit.EditLink(webUrl, urlbook1);
										 getBookInfo(t,turlbook1);
									 
									 for(int chiso2=1;chiso2<list_book.size();chiso2++)
									 {
										 String urlbook2=list_book.get(chiso2).attr("href");
										 if(urlbook2==null) continue;
										 String u1=urlbook1.replaceAll("/","");
										 String u2=urlbook2.replaceAll("/", "");
										 if(u1.equals(u2))
										 {
											 continue;
										 }
										 else
										 {
											 String turlbook2=StringEdit.EditLink(webUrl, urlbook2);
											 getBookInfo(t,turlbook2);
											 if(flagBook==true) 
												 {
												 	urlbook1=urlbook2;
												 	flagBook=false;
												 }
										 }
										 
										 
									 }
								 }
								 
								 
							 }
			
							

						 }
					 }
					 		 					
				}
				else
				{
					// khong co noi dung tu link, khong co trang
					List<Element> list_node2 = con.executeXpath(root1, xp_web, 0);
					if(list_node2!=null&&list_node2.size()>0)
					{
						Element web_con=list_node2.get(0);
						 List<Element> list_book=web_con.getElementsByTag("a");
						 if(list_book!=null && list_book.size()>0) 
						 {
							 String urlbook1=list_book.get(0).attr("href");
								String turlbook1=StringEdit.EditLink(webUrl, urlbook1);
								 getBookInfo(t,turlbook1);
							 for(int index=0;index<list_book.size();index++)
							 {
								 String urlbook2=list_book.get(index).attr("href");
								 if(urlbook2==null) continue;
								 String u1=urlbook1.replaceAll("/","");
								 String u2=urlbook2.replaceAll("/", "");
								 if(u1.equals(u2))
								 {
									 continue;
								 }
								 else
								 {
									 String turlbook2=StringEdit.EditLink(webUrl, urlbook2);
									 getBookInfo(t,turlbook2);
									 if(flagBook==true) 
									 {
										 	urlbook1=urlbook2;
										 	flagBook=false;
									 }
								 }
							 }
						 }
					}
				}
			}
		}
	}
	
	public void getBookInfo(Link t,String url)
	{
		if(url.length()<6)return;
		
		 url= StringEdit.EditLink(webUrl, url);
		 String xp_img=t.getWebsite().getImage();
		 String xp_book_con =t.getWebsite().getBookContent();
		 String xp_book_info= t.getWebsite().getBookInfo();
		 
		 ConnectionWeb con= new ConnectionWeb();
		 Document root = con.getInfo(url);
		 
		 String sbook_info="";
		 List<Element> l_book_info=con.executeXpath(root,xp_book_info,5);
		 if(l_book_info==null||l_book_info.size()==0) return;
		 Element book_info=l_book_info.get(0);
		 
		 sbook_info=book_info.text();
		/* for(int chiso=0;chiso < l_book_info.size();chiso++)
		 {
			 Element tbook_info=l_book_info.get(chiso);
			 // lay duoc node cha cua cac node thong tin
			 List<?> l_temp=tbook_info.getChildTagList();
			 
			 for(int chiso2=0;chiso2 < l_temp.size();chiso2++)
			 {
				 Element child_con= (Element) l_temp.get(chiso2);
				 sbook_info=sbook_info +" " + child_con.getText().toString(); 
			 }
			 
		 }*/
		 
		 //goi ham luu sach
		 ExtractInfoBook extract= new ExtractInfoBook();
		 
		 List<Element> l_img=con.executeXpath(root, xp_img, 0);
		 if(l_img==null||l_img.size()==0) return;
		 
		 List<Element> l_book_con;
		 String book_con=null;
		if(xp_book_con==null||xp_book_con.equals("null")||xp_book_con.equals(""))
		{
			book_con="";
		}
		else
		{	 l_book_con=con.executeXpath(root, xp_book_con, 0);
			if(l_book_con==null||l_book_con.size()==0) return;
			Element book_content=l_book_con.get(0);	
			sbook_info=sbook_info.replaceAll("&nbsp;"," ");
			book_con=book_content.toString();
		}
		
		Book book= extract.ParseString(book_con, sbook_info);
		LinkDAO linkdao= new LinkDAO();
		Link tlink=linkdao.getlink(linkGetBook.getLinkUrl());
		book.setBookUrl(url);
		book.setTopic(t.getTopic());
		book.setView(10);
		BookDAO bookdao= new BookDAO();
		int numid=bookdao.insert(book);
		System.out.println(numid);
		if(numid == -1){
			return;
		}
			
		flagBook=true;
		
		 if(l_img==null||l_img.size()==0) return;
		 Element t_img=l_img.get(0);
		 
		 List<Element> l_image= t_img.getElementsByTag("img");
		 if(l_image!=null&&l_image.size()!=0)
		 {
			 Element img= t_img.getElementsByTag("img").get(0);
			 String imgurl=img.attr("src");
			 
			 if(imgurl.indexOf("http")==-1)
				 imgurl=webUrl+imgurl;
			 URL iurl;
			 boolean flag=true;
			try {
				iurl = new URL(imgurl);
				URLConnection cnx = iurl.openConnection();
				cnx.setAllowUserInteraction(false);         
				cnx.setDoOutput(true);
				cnx.setReadTimeout(2000);
				cnx.addRequestProperty("User-Agent", 
					    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
				
				cnx.addRequestProperty("Content-Type","application/octet-stream");	
				InputStream is;
				is = cnx.getInputStream();
				String dir="E:\\"+Integer.toString(numid)+".jpg";
				OutputStream os = new FileOutputStream(dir);
	
				byte[] b = new byte[2048];
				int length;
	
				while ((length = is.read(b)) != -1) {
					os.write(b, 0, length);
				}
	
				is.close();
				os.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				flag=false;
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				
				flag=false;
				e.printStackTrace();
			}
			String bookImage;
			if(flag==false)
			{
				System.out.println("no image");
				bookImage="Content/images/book/default.jpg";
			
			}
			else
			{
				bookImage="Content/images/book/"+ Integer.toString(numid)+".jpg";
			}
			
			book.setImageUrl(bookImage);
			bookdao.update(book);
		 }
		 else
		 {
			String bookImage="Content/images/book/default.jpg";				
			book.setImageUrl(bookImage);
			bookdao.update(book);
		 }
	}

}
